package com.wt.springsamples.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wt.springsamples.biz.service.ISysConfigService;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.model.SysMenu;
import com.wt.springsamples.model.SysRole;
import com.wt.springsamples.model.dto.RoleFuncConfigDto;
import com.wt.springsamples.model.dto.RoleMenuConfigDto;
import com.wt.springsamples.model.search.UserSearchModel;
import com.wt.springsamples.biz.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("角色管理")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysConfigService sysConfigService;

    @GetMapping("/{id}")
    @ApiOperation(value = "role:view",nickname = "角色管理-查看")
//    @PreAuthorize("hasAuthority('role:view')")
    public ApiResult getById(@PathVariable("id") Long id) {
        System.out.println("role:view");
        SysRole role = sysRoleService.getById(id);
        return ApiResult.ok(role);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "role:delete",nickname = "角色管理-删除")
//    @PreAuthorize("hasAuthority('role:delete')")
    public ApiResult deleteById(Long id) {
        sysRoleService.deleteById(id);
        return ApiResult.ok();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "role:delete",nickname = "角色管理-批量删除")
//    @PreAuthorize("hasAuthority('role:delete')")
    public ApiResult deleteByIds(@RequestBody List<Long> ids) {
        System.out.println("role:delete");
        sysRoleService.deleteByIds(ids);
        return ApiResult.ok();
    }

    @PostMapping("/create")
    @ApiOperation(value = "role:create",nickname = "角色管理-新建")
//    @PreAuthorize("hasAuthority('role:create')")
    public ApiResult create(@RequestBody  SysRole sysRole) {
        System.out.println("role:create");
        sysRoleService.save(sysRole);
        return ApiResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value = "role:update",nickname = "角色管理-更新")
//    @PreAuthorize("hasAuthority('role:update')")
    public ApiResult update(SysRole sysRole) {
        System.out.println("role:update");
        sysRoleService.update(sysRole);
        return ApiResult.ok();
    }

    @RequestMapping("/listAll")
    @ApiOperation(value = "role:query",nickname = "角色管理-查询全部")
//    @PreAuthorize("hasAuthority('role:query')")
    public ApiResult listAll(){
        List<SysRole> list = sysRoleService.list();
        return  ApiResult.ok(list);
    }

    @RequestMapping("/queryList")
    @ApiOperation(value = "role:query",nickname = "角色管理-分页查询")
//    @PreAuthorize("hasAuthority('role:query')")
    public ApiResult queryList(
            @RequestParam(required = false,defaultValue ="1") Integer currentPage,
            @RequestParam(required = false,defaultValue ="5") Integer pageSize,
            UserSearchModel searchModel){
        Page<SysRole> page = sysRoleService.selectPage(searchModel,currentPage, pageSize);
        return  ApiResult.ok(page);
    }

    @PostMapping("/selectRoleMenus")
    @ApiOperation(value = "role:configuredMenuList",nickname = "角色管理-已配置权限列表")
    public ApiResult selectRoleMenus(Long roleId){
        List<Long> list = sysRoleService.getConfigMenuList(roleId);
        return  ApiResult.ok(list);
    }

    @PostMapping("/configPermission")
    public ApiResult configPermission(RoleFuncConfigDto roleFuncConfigDto){
        System.out.println("role:config");
        sysRoleService.config(roleFuncConfigDto.getRoleId(), roleFuncConfigDto.getFuncIdList());
        return  ApiResult.ok();
    }

    @PostMapping("/config")
    @ApiOperation(value = "role:config",nickname = "角色管理-权限配置")
    public ApiResult viewConfig(@RequestBody RoleMenuConfigDto roleMenuConfigDto){
        System.out.println("role:config");
//        sysRoleService.viewConfig(1L, Arrays.asList(1L,2L,3L,4L));
        sysRoleService.viewConfig(roleMenuConfigDto.getRoleId(), roleMenuConfigDto.getMenuIdList());
        return  ApiResult.ok();
    }
}
